Up until now, you may have taken LNK (link) files for
granted in Windows because they're exceptionally easy to create.
Unfortunately, without a GUI in Server Core, it's become significantly
harder to create LNK files. You'll find a ton of references to an
elusive Shortcut utility for Windows online, but you won't find the
actual executable on your hard drive, which may leave you puzzling for a
long time. It's possible to create LNK files using scripts. Listing 1
shows a script that places a LNK file on your Desktop for Notepad. Now
you can double-click this LNK file and open a copy of Notepad (menu not
required).
Server Core may not display links on the Desktop. In this case, you can still place links in a common folder, such as %USERPROFILE%,
which points to your user folder. You can still type the LNK filename
and press Enter to use the link file from the command line. In short,
LNK files are still useful, even if you can't see their physical
manifestation in the form of an icon.
|
|
Example 1. Creating a New LNK File
' Create the shell object. Set WshShell = WScript.CreateObject("WScript.Shell")
' Define the location of the LNK file. LinkFilename = WshShell.ExpandEnvironmentStrings("%USERPROFILE%") LinkFilename = LinkFilename + "\Desktop\Notepad.LNK"
' Create the LNK file object. Set LNKFile = WshShell.CreateShortcut(LinkFilename)
' Set the LNK file contents. LNKFile.TargetPath = "%SYSTEMROOT%\System32\Notepad.EXE" LNKFile.Arguments = "" LNKFile.Description = "Open Notepad" LNKFile.HotKey = "" LNKFile.IconLocation = "%SYSTEMROOT%\System32\Notepad.EXE, 1" LNKFile.WindowStyle = "1" LNKFile.WorkingDirectory = "C:\"
' Save the LNK file to disk. LNKFile.Save
|
The code begins by
creating a Windows Script shell. This shell provides access to features
such as environment strings and the function for creating a shortcut.
The next step creates a location name based on the user's profile—the \Users\UserName
folder for the user. You may use any of the environment variables that
Windows supports to obtain information for your scripts. It then adds
the actual LNK file location to the path.
Now that the code has a path to use for creating the LNK file, it uses the CreateShortcut()
function to create it. At this point, the script sets the various LNK
file contents. Any field you can change in a LNK file, you can also
change using a script. Finally, the script saves the results to the hard
drive. It's essential to perform this final step or LNK file won't
work. Simply type the LNK filename and press Enter to execute it at the
command line. For example, in this case you would type Notepad.LNK and press Enter at the command line to start a copy of Notepad.
You can find a downloadable version of the Shortcut command at http://www.optimumx.com/download/. The Shortcut
command provides the means to create a LNK file without resorting to
using a script. In addition, you can use it to create an INI file that
contains all of the LNK file configuration information. The ReadMe.TXT file that comes with the Shortcut command tells you about all of the command line parameters you need to use it. |